<template>
  <div class="app-container">
    <!-- 查询表单-->
    <!--element-ui的 date-picker组件默认绑定的时间值是默认世界标准时间，和中国时间差8小时
          设置 value-format="yyyy-MM-dd HH:mm:ss" 改变绑定的值-->
    <div id="content">
      <el-row>
        <el-col>
          <div class="grid-content bg-purple">
            <i class="el-icon-menu">优惠券列表</i>
          </div>
        </el-col>
        <el-col><div class="grid-content bg-purple-light"></div></el-col>
        
        <el-col>
          <div class="grid-content bg-purple add">
              <router-link :to="'/mysetting/addCoupon'">
                <el-button type="primary" icon="el-icon-circle-plus-outline">添加优惠券</el-button>
              </router-link>
          </div>
        </el-col
        >
      </el-row>
      <p />
      <!--表格-->
      <el-table
        v-loading="listLoading"
        :data="list"
        @selection-change="handleSelectionChange"
        element-loading-text="数据加载中"
        border
        fit
        stripe
        highlight-current-row
        style="width: fit-content"
      >
        <!--数据展示-->
        <el-table-column prop="type" label="类型" width="160" />
        <el-table-column prop="condition" label="满减底线" width="100" />
        <el-table-column prop="value" label="面值" width="130" />
        <el-table-column prop="createTime" label="创建时间" width="160" />
        <el-table-column prop="expirationTime" label="过期时间" width="160" />
        <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope">
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="removeDataById(scope.row.id)"
              >下架</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <br/>

      <!-- 分页 -->
      <el-pagination
        :current-page="page"
        :page-size="limit"
        :total="total"
        background
        style="padding: 30px 0; text-align: center"
        layout="total, prev, pager, next, jumper"
        @current-change="selectAllToShow"
      />
    </div>
  </div>
</template>
<script>
//引入调用Teacher.js文件
import couponApi from "@/api/coupon";

export default {
  //写核心代码位置
  // data:{
  // },
  data() {
    //定义变量和初始值
    return {
      coupon: {},
      listLoading: true, // 是否显示loading信息
      list: null, //查询之后接口返回集合
      page: 1, //当前页
      limit: 5, //每页记录数
      total: 0, //总记录数
      pageNum: 0,//当前页
      pageSize: 0,//总页数
      select: "", //搜索条件
      checkList: [],
      myPage: {
        	pageNum: 1,
        	pageSize: 10,
      },
      pageNum: 1,
      pageSize: 10,
      
    };
  },
  created() {
    //页面渲染之前执行，一般调用methods定义的方法
    //调用
    this.selectAllToShow();
  },
  mounted() {
    this.listLoading = false;
    
  },
  methods: {
    //获取所有优惠券的方法,默认是第一页
    selectAllToShow() {
      couponApi
        .selectAll(this.pageNum,this.pageSize)
        .then((response) => {
          //请求成功
          //response接口返回的数据  
           console.log(5555)
          this.list = response.data.list;
          this.total = response.data.total;
          this.pageNum = response.data.pageNum;
          this.pageSize = response.data.pageSize;
        });
    },
    //下架的方法
    removeDataById(coupon) {
      this.$confirm(
        "此操作将会删除数据,您仍然可通过回收站找回 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          //点击确定，执行then方法          
          //调用删除的方法
          couponApi.deleteById(coupon).then((response) => {
            //删除成功
            //提示信息
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            //回到列表页面
            this.selectAllToShow();
          });
        })
        .catch((e) => {
          this.$message({
            type: "info",
            message: "已取消删除!",
          });
        }); //点击取消，执行catch方法
    },
  },
};
</script>
<style>
#content {
  width: 80%;
  margin: 0 auto;
}
.el-row .el-col-24 {
  width: 150px;
}
.el-col-24:nth-child(3),
.el-col-24:nth-child(4) {
  float: right;
}
</style>
